﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChurchData.Business;

namespace ChurchData
{
    public partial class AddDialog : Form
    {
        private ErrorProvider errors;
        public int NewPeoplePK { get; set; }

        public AddDialog(string firstName, string lastName)
        {
            InitializeComponent();
            if (firstName.Length > 0) firstName = firstName.Substring(0, 1).ToUpper() + firstName.Substring(1, firstName.Length - 1);
            if (lastName.Length > 0) lastName = lastName.Substring(0, 1).ToUpper() + lastName.Substring(1, lastName.Length - 1);
            this.txtFirstName.Text = firstName;
            this.txtLastName.Text = lastName;
            this.DialogResult = DialogResult.Cancel;
            errors = new ErrorProvider();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnContinueAdd_Click(object sender, EventArgs e)
        {
            bool hasErrors = false;

            errors.Clear();
            errors.BlinkStyle = ErrorBlinkStyle.NeverBlink;

            if (this.txtFirstName.Text.Trim() == "")
            {
                errors.SetError(this.txtFirstName, "Required");
                hasErrors = true;
            }
            if (this.txtLastName.Text.Trim() == "")
            {
                errors.SetError(this.txtLastName, "Required");
                hasErrors = true;
            }

            if (hasErrors) return;

            PeopleBO pBO = new PeopleBO();
            this.NewPeoplePK = pBO.AddPerson(this.txtFirstName.Text.Trim(), this.txtLastName.Text.Trim());

            this.DialogResult = DialogResult.OK;
        }
    }
}
